//
//  SetPassWordController.m
//  quickLook
//
//  Created by J on 15/9/3.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "SetPassWordController.h"
#import "Header.h"
#import "AFHTTPRequestOperationManager.h"

#import "LandController.h"

@interface SetPassWordController ()
@property (weak, nonatomic) IBOutlet UITextField *nickName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;

@property (weak, nonatomic) IBOutlet UIButton *finish;
@property(nonatomic,strong)UIAlertView* alertView;

@end

@implementation SetPassWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _finish.layer.cornerRadius=3;

}
- (IBAction)finish:(UIButton*)sender {
    
    
    
    if (_nickName.text&&_passWord.text) {
        
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSDictionary *params = @{@"nickname":_nickName.text, @"password":_passWord.text};
        JZJLog(@"%@%@",_passWord,_nickName);
        
        [manager POST:@"http://api.kuaikanmanhua.com/v1/phone/signup" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            JZJLog(@"%@",responseObject[@"message"]);
            
            
            if ([responseObject[@"message"] isEqualToString:@"invalid phone number"]) {
                
                
                [self Alert];
                
                
            }else if ([responseObject[@"message"] isEqualToString:@"ok"])
            {
                
                
                NSDictionary* dic=responseObject[@"data"];
                
                NSDictionary* dataDIc=@{@"phonNumber":_phonNumber,@"avatar_url":dic[@"avatar_url"],@"nickname":dic[@"nickname"],@"passWord":_passWord.text,@"id":dic[@"id"]};
                
                
                LandController* vc=[[LandController alloc]init];
                
                
                NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
                
                [userdefault setObject:dataDIc forKey:DATADIC];
                
                [userdefault synchronize];
                
                vc.dataDic=dataDIc;
                [self.navigationController pushViewController:vc animated:YES];
                
                
            }else
            {
            
                SetPassWordController* vc=[[SetPassWordController alloc]initWithNibName:@"SetPassWordController" bundle:nil];
                    vc.navigationItem.title=@"设置密码";
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
            
            JZJLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JZJLog(@"Error: %@", error);
        }];
        
        
        
        
        
    }
    
    

}

- (void)Alert {
    _alertView = [[UIAlertView alloc] initWithTitle:nil
                                            message:@"验证码无效或者过期了"
                                           delegate:nil
                                  cancelButtonTitle:nil otherButtonTitles:nil, nil];
    
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(dismissAlertView:)
                                   userInfo:nil
                                    repeats:NO];
    
    [_alertView show];
    
}


- (void)dismissAlertView:(NSTimer*)timer {
    
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
